package com.zhugang.week06;

import java.util.ArrayList;
import java.util.List;

/**
 * @program algorithms
 * @description: postorderTraversal
 * @author: chanzhugang
 * @create: 2022/07/13 11:28
 */
public class PostorderTraversal {


    /**
     * 145 二叉树的后序遍历
     *
     * @return
     */

    List<Integer> res = new ArrayList<>();

    public List<Integer> postorderTraversal(TreeNode root) {
        postorder(root);
        return res;
    }

    private void postorder(TreeNode root) {
        if (root == null) {
            return;
        }
        postorder(root.left);
        postorder(root.right);
        res.add(root.val);
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}